<% (chart['template'] || []).each do |variable| %>
  <% if variable['hidden'] %>
    <% if form.object.new_record? %>
      <!-- Only set this on new records because existing records will already have it. -->
      <%= form.hidden_field(
        "metadata][#{chart['name']}][template][#{variable['key']}",
        value: variable['default']
      ) %>
    <% end %>
  <% else %>
  <div class="form-group">
    <% if variable['type'] == 'string' %>
      <%= form.label variable['key'], variable['name'] %>
      <%= form.text_field(
        variable['key'],
        name: "add_on[metadata][#{chart['name']}][template][#{variable['key']}]",
        class: "input input-bordered w-full max-w-md",
        value: form.object.metadata.dig('template', variable['key']) || variable['default']
      ) %>
    <% elsif variable['type'] == 'boolean' %>
      <label class="label cursor-pointer mt-1">
        <%= form.check_box(
          variable['key'],
          name: "add_on[metadata][#{chart['name']}][template][#{variable['key']}]",
          class: "checkbox",
          checked: form.object.metadata.dig('template', variable['key']) || variable['default']
        ) %>
        <span class="label-text ml-2">
          <%= variable['name'] %>
        </span>
      </label>
    <% elsif variable['type'] == 'integer' %>
      <%= form.label variable['key'], variable['name'] %>
      <%= form.number_field(
        variable['key'],
        name: "add_on[metadata][#{chart['name']}][template][#{variable['key']}]",
        class: "input input-bordered",
        value: form.object.metadata.dig('template', variable['key']) || variable['default']
      ) %>
    <% elsif variable['type'] == 'enum' %>
      <%= form.label variable['key'], variable['name'] %>
      <%= form.select "metadata][#{chart['name']}][template][#{variable['key']}", options_for_select(variable['options']), {}, class: "select select-bordered w-full max-w-md" %>
    <% elsif variable['type'] == 'size' %>
      <%= form.label variable['name'] %>
      <%= form.hidden_field(
        "metadata][#{chart['name']}][template][#{variable['key']}][type",
        value: 'size'
      ) %>
      <%= form.text_field(
        variable['key'],
        name: "add_on[metadata][#{chart['name']}][template][#{variable['key']}][value]",
        class: "input input-bordered",
        type: 'number',
        value: form.object.metadata.dig('template', variable['key'], 'value') || variable['default'][..-3]
      ) %>
      <%= form.select(
        "metadata][#{chart['name']}][template][#{variable['key']}][unit",
        options_for_select([['Gi', 'Gi'], ['Mi', 'Mi'], ['Ki', 'Ki']]),
        { value: form.object.metadata.dig('template', variable['key'], 'unit') || variable['default'][-2..] },
        { class: "select select-bordered" }
      ) %>
      <% end %>
    </div>
  <% end %>
<% end %>